package BookReadIO;


import BasicClass.Book;
import javafx.scene.control.Alert;
import javafx.stage.StageStyle;

import java.io.*;
import java.util.ArrayList;

public class BookWriter {
    public ArrayList<Book> ReadTxt(File file) throws IOException {
        if (file==null) return null;
        BufferedReader fileReader = new BufferedReader(new FileReader(file));
        ArrayList<Book> arrayList = new ArrayList<>();
        String str;
        String[] strings;
        while ((str = fileReader.readLine())!=null){
            strings = str.split(",", -1);
            if(strings.length!=10) {
                Alert alert = new Alert(Alert.AlertType.ERROR);
                alert.setTitle("错误");
                alert.setHeaderText("你所给文件格式错误");
                alert.initStyle(StageStyle.UTILITY);//不显示图标
                alert.show();
                break;
            }
            else{
                Book book = new Book(
                        strings[0], strings[1], strings[2], strings[3],
                        Integer.parseInt(strings[4]), Double.valueOf(strings[5]),
                        strings[6], strings[7], strings[8]
                );
                arrayList.add(book);
            }
        }
        return arrayList;
    }
}
